package service;

import dao.db;
import pojo.Guest;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class guestService {

    public static int insert(Guest guest) {
        db d = new db();
        String sql = "INSERT INTO hotel.guest (bname, bsex, info)\n" +
                "VALUES ('" + guest.getBname() +
                "', '" + guest.getBsex() +
                "', '" + guest.getInfo() +
                "');";
        System.out.println(sql);
        return d.update_execute(sql);
    }

    public static List<Guest> all() {
        db d = new db();
        String sql = "select * from Guest";
        ResultSet resultSet = d.executeQuery(sql);
        List<Object> list = db.selectList(new Guest(), resultSet);
        List<Guest> list1 = new ArrayList<>();
        list.forEach(i -> {
            list1.add((Guest) i);
        });
        return list1;
    }

    public static List<Guest> queryByid(int id) {
        db d = new db();
        String sql = "select * from Guest where id=" + id;
        ResultSet resultSet = d.executeQuery(sql);
        List<Object> list = db.selectList(new Guest(), resultSet);
        List<Guest> list1 = new ArrayList<>();
        list.forEach(i -> {
            list1.add((Guest) i);
        });
        return list1;
    }

    public static List<Guest> queryByName(String name) {
        db d = new db();
        String sql = "select * from Guest where bname like '%" + name + "%'";
        System.out.println(sql);
        ResultSet resultSet = d.executeQuery(sql);
        List<Object> list = db.selectList(new Guest(), resultSet);
        List<Guest> list1 = new ArrayList<>();
        list.forEach(i -> {
            list1.add((Guest) i);
        });
        return list1;
    }

    public static int deleteByid(int id) {
        db d = new db();
        String sql = "delete from Guest where id=" + id;
        return d.update_execute(sql);
    }

    public static int updateByid(Guest guest) {
        db d = new db();
        String sql = "UPDATE hotel.guest t\n" +
                "SET t.bname      = '" + guest.getBname() +
                "',\n" +
                "    t.bsex       = '" + guest.getBsex() +
                "',\n" +
                "    t.is_in_room = '" + guest.getIsInRoom() +
                "',\n" +
                "    t.info       = '" + guest.getInfo() +
                "'\n" +
                "WHERE t.id = " + guest.getId() +
                ";";
        return d.update_execute(sql);
    }
}
